﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControls_ProductsList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PopulateControls();
    }

    private void PopulateControls()
    {
        // Retrieve EmpresaID from the query string
        string empresaId = Request.QueryString["EmpresaID"];
        // Retrieve CategoryID from the query string
        string categoryId = Request.QueryString["categoriaID"];
        // Retrieve Page from the query string
        string page = Request.QueryString["Page"];
        string pageEmp = Request.QueryString["PageEmp"];
        if (page == null) page = "1";
        if (pageEmp == null) pageEmp = "1";
        // Retrieve Search string from query string
        string searchString = Request.QueryString["Search"];
        // How many pages of products?
        int howManyPages = 1;
        // pager links format
        string firstPageUrl = "";
        string pagerFormat = "";

        // If performing a product search
        if (searchString != null)
        {
        // Retrieve AllWords from query string
            string allWords = Request.QueryString["AllWords"];
            // Perform search
            list.DataSource = CatalogAccess.SearchProdutos(searchString, allWords, page, out howManyPages);
            list.DataBind();
            if (list.Items.Count == 0)
            {
                quantidadeLabel.Visible = true;
                quantidadeLabel.Text = "Nenhuma ocorrência encontrada.";
            }
            // Display pager
            firstPageUrl = Link.ToSearch(searchString, allWords.ToUpper() == "TRUE", "1", "1");
            pagerFormat = Link.ToSearch(searchString, allWords.ToUpper() == "TRUE", "{0}", pageEmp);
        }

        else if (empresaId != null)
        {
            // Retrieve list of products
            list.DataSource = CatalogAccess.GetProdutosDaEmpresa(empresaId, page, out howManyPages);
            list.DataBind();
            if (list.Items.Count > 0)
            {
                produtosEmpresa.Text = "<p class=\"prodserv\" style=\"font-size: large; font-weight: bold; text-align: center\"><br />Produtos ou serviços deste anunciante</p>";
                produtosEmpresa.Visible = true;
            }
                 
            // get first page url and pager format
            firstPageUrl = Link.ToCompanyWithoutRewritter(empresaId, "1");
            pagerFormat = Link.ToCompanyWithoutRewritter(empresaId, "{0}");
        }

        else
        {
            // Retrieve list of empresas on catalog promotion
            list.DataSource = CatalogAccess.GetEmpresasEmFrontPromo(page, out howManyPages);
            list.DataBind();
            // have the current page as integer
            int currentPage = Int32.Parse(page);
            firstPageUrl = Link.ToDefaultWithoutRewritter("1");
            pagerFormat = Link.ToDefaultWithoutRewritter("{0}");

        }

        // Display pager controls
        topPager.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, false);
        bottomPager.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, true);
    }
}
